package com.ngkj.zmfptback.sys.controller;

import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.sys.bean.MenuEntity;
import com.ngkj.zmfptback.sys.config.JwtConfig;
import com.ngkj.zmfptback.sys.service.read.MenuReadService;
import com.ngkj.zmfptback.sys.service.write.MenuWriteService;
import com.ngkj.zmfptback.sys.util.JwtTokenUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ChenHN
 * @since 2021/12/15 creation
 **/
@RestController
public class MenuController {

    @Resource
    private MenuReadService menuReadService;

    @Resource
    private MenuWriteService menuWriteService;

    @GetMapping("/getMenu")
    public CommonReslut getMenu(HttpServletRequest request){
        String headerToken = request.getHeader(JwtConfig.tokenHeader);
        List<String> roleTrueList = new ArrayList();
        if (StringUtils.isEmpty(headerToken)) {
            return new CommonReslut("未登录",300);
        }else {
            //截取jwt前缀
            String token = headerToken.replace(JwtConfig.tokenPrefix, "");
            Map<String, Object> userInfo = JwtTokenUtil.getUserInfo(token);
            List<String> roleList = (List<String>) userInfo.get("roleList");
            roleList.forEach(item->{
                roleTrueList.add(item.split("_")[1]);
            });
        }
        List<Map<String, Object>> menuTree = menuReadService.getMenuTree();
        List<Map<String, Object>> menuTree1 = menuReadService.getMenuTree(roleTrueList);
        return new CommonReslut(ResultEnum.ok,menuTree1);
    }

    @GetMapping("/menu/getMenuTree")
    public CommonReslut getMenuTree(){
        List<Map<String, Object>> menuTree = menuReadService.getMenuTree();
        return new CommonReslut(ResultEnum.ok,menuTree);
    }

    @GetMapping("/menu/getMaxOrder")
    public CommonReslut getMaxOrder(Integer id){
        int maxOrderById = menuReadService.getMaxOrderById(id);
        return new CommonReslut<>(ResultEnum.ok,maxOrderById);
    }

    @DeleteMapping("/menu/deleteMenu")
    public CommonReslut deleteMenu(Long id){
        try {
            menuWriteService.deleteMenuById(id);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/menu/addMenu")
    public CommonReslut addMenu(@RequestBody MenuEntity menuEntity){
        try {
            menuWriteService.addMenu(menuEntity);
            return new CommonReslut<>(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut<>(ResultEnum.error);
        }
    }
    @PostMapping("/menu/editMenu")
    public CommonReslut editMenu(@RequestBody MenuEntity menuEntity){
        try {
            menuWriteService.editMenu(menuEntity);
            return new CommonReslut<>(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut<>(ResultEnum.error);
        }
    }

}
